<?php
namespace App\Libs\Common;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class ZtwssController extends Controller
{	

     //郑投网  (传用户和订单信息)
    public  function ztw_user_order($order_id)
    {  
        header("Content-type: text/html; charset=utf-8");
        $field="a.user_province,a.user_name,a.user_phone,a.user_address,a.goods_id,a.order_create_time,a.zm_credit_score,a.order_tenancy_term,a.user_id_number,a.order_installation_charge,a.order_monthly_rent,b.bankcard";
       
        $row=M("orders as a")->join('user as b on a.user_phone=b.phone')->field($field)->where("a.order_id='$order_id'")->find();
        
        //"H5支付宝"
        $param['id']=$order_id;//订单号
        $param['name']=$row["user_name"];//用户名
        $param['phone']=$row["user_phone"];//手机号
        $param['city']=$row["user_province"];//城市
        $param['address']=$row["user_address"];//街道
        $param['goodsid']=$row["goods_id"];//商品ID
        $param['createtime']=$row["order_create_time"];//订单创建时间
        $param['data1']=$row["zm_credit_score"];//信用分
        $param['data3']=$order_id;//信用借还id
        $param['data6']=$row["order_tenancy_term"];//租期(月)
        $param['data7']=$row["user_id_number"];//身份证
        $param['isnew']='10';//商品新旧程度
        $param['data10']=$row["order_installation_charge"];//安装包金额
        $param['data12']=$row["order_monthly_rent"];//月租(每月价格）
        $param['data14']=$row["bankcard"];//银行卡号
      
        $url='http://117.158.208.96:10102/thirdPerson/personalInfo';
        $param=json_encode($param,JSON_UNESCAPED_UNICODE);
    
        $result=$this->request_post($url,$param);
       
        $ztw_res=json_decode($result,true);
        
		echo "<pre>";
		print_r($ztw_res);
		echo "</pre>";
		
        if ($ztw_res["status"] == 'SUCCESS') 
        { 
            $res=M("orders")->where("order_id= '$order_id'")->save(array("is_delivery"=>2));
            if (!$res) 
            {
                echo "修改订单表状态失败";
            }
            $ztw_risk=$this->ztw_risk($row["user_phone"]); 
        }else{

            echo "传递用户和订单信息失败";
        }
        
    }


    //郑投网  (传行业关注名单)
    public function ztw_risk($phone)
    {   
        header("Content-type: text/html; charset=utf-8");
        $row=M("user")->where("phone='$phone'")->field("name,idcard,openid")->find();
        $param['name']=$row["name"];//用户名
        $param['data7']=$row["idcard"];//身份证号

        $ZhimaH5=A("OutApi/ZhimaH5");       
        $res=$ZhimaH5->zhimaCreditWatchlistiiGet($row["openid"]);
        $param['industryfocus']=json_encode($res,JSON_UNESCAPED_UNICODE);//行业关注名单查询信息
        
        $url='http://117.158.208.96:10102/thirdPerson/personalRisk';
        $param=json_encode($param,JSON_UNESCAPED_UNICODE);
        
        $result=$this->request_post($url,$param);
        $ztw_risk_res=json_decode($result,true);  
        
        if($ztw_risk_res["status"] == 'SUCCESS')
        {
            echo "SUCCESS";
        }else{
            echo "传递行业关注名单信息失败";
        }
    }

    public function test()
    {   header("Content-type: text/html; charset=utf-8");
        $order_id=I("order_id");
        if (empty($order_id)) exit("参数不能为空");
        $this->ztw_user_order($order_id);   
    }

    public function test2()
    {     header("Content-type: text/html; charset=utf-8");
         $id=I("id");
         if (empty($id)) exit("参数不能为空");
         $this->ztw_order_stages($id);
    }

    //郑投网  (分期数据)  
    public function ztw_order_stages($id)
    {   
        //$id 分期表id
        header("Content-type: text/html; charset=utf-8");
      
        $field='a.order_id,a.monthly_rent,a.paid_monthly_rent,b.order_create_time,a.repayment_time,a.last_repayment_time,b.goods_id,b.order_gross_rent,b.is_delivery,a.days_overdue,a.amount_override,a.fq_status,a.current_tenancy_term,a.ztw_status';
        $row=M("order_stages as a")->join("orders as b on a.order_id=b.order_id")->where("a.id='$id'")->field($field)->find();
        
        $param['payDetail']=0;//支付状态（0未付款1已付款）
        $param['id']=$id;//商户订单号 分期表ID
        $param['orderId']=$row["order_id"];//订单号
        $param['amountNeed']=$row["monthly_rent"];//应付金额 （月租）
        $param['amountAlready']=$row["paid_monthly_rent"];//已付金额  
        $param['createTime']=$row["order_create_time"];//创建时间 (下单时间)
        $param['lastPaytime']=$row["repayment_time"];//最后还款时间  (每月还款时间)
        $param['payTime']=$row["last_repayment_time"];//还款时间  
        $param['couponOid']='0';//优惠券id 
        $param['data1']=$row["goods_id"];//商品id  
        $param['data2']=$row["order_gross_rent"];//全额款 (总租金)
        $param['data3']=$row["days_overdue"];//逾期天数
        $param['data4']=$row["amount_override"];//逾期金额 
        $param['number']=$row["current_tenancy_term"];//当前期数

        if ($row["fq_status"] == 2) 
        {  
            $param['payDetail']=1;//支付状态（0未付款1已付款） 
        }

        //已传递过的数据不再重复传递或非郑投网订单
        if ($row['ztw_status']==2 || $row['is_delivery']==1) return false;

        $url='http://117.158.208.96:10102/thirdTrade/payItem';
        $param=json_encode($param,JSON_UNESCAPED_UNICODE);
   
        $result=$this->request_post($url,$param);
        
        //修改 ztw_status 状态
        $ztw_res=json_decode($result,true);

        if ($ztw_res["status"] == 'SUCCESS')
        {   
            $data["ztw_status"]=2;
            $res=M("order_stages")->where("id='$id'")->save($data);
            if ($res) {
                echo "SUCCESS";
            }else{
                echo "修改分期表状态失败";
            }
        }else{
            echo "传递分期数据信息失败";
        }
    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
    /**
     * 模拟POST请求
     * @param  string $url   [请求地址]
     * @param  string $param [参数]
     */
    private function request_post($url = '', $param = '') 
    {      
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_URL,$url);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS,$param);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array(
                'Content-Type: application/json; charset=utf-8',
        ));

        $data = curl_exec($ch);//运行curl

        if($data === false)
        {
            return  'Curl error: ' . curl_error($ch);
        }
        
        curl_close($ch);
        
        return $data;
    }




}